
function ChartController($scope, $http) {

    $scope.get_coms = function(com_name) {
        var api = "/v1/nexus/components/query?c_name=" + com_name;
        var url = protocol + host + ":" + port + api;
        $http.get(url).success(function(response) {
            console.log(response);
        });
    }
}

function ScopelineInstDetailController($scope, $http, $routeParams) {

}

function ShowScopeInstController($scope, dataFactory) {

    $scope.selected_env_name='AliHangzhou';

    var start = 1;
    var size = 1000;

    dataFactory.get_envs(start, size).success(function(data) {
        $scope.envs = data.envs;
    });
//    $scope.firstItemView = true;
    $scope.$watch('selected_env_name', function() {

        dataFactory.get_scopelines_by_envname($scope.selected_env_name, $scope.start_point, $scope.itemsPerPage).success(function(data) {
//            $scope.firstItemView = true;
            $scope.firstItemView = true;
            $scope.totalItems = data.Count;
            $scope.scopeline_list = data.Scopelines;
            $scope.firstItem = $scope.scopeline_list.shift();
        });
    });

    $scope.$watch('currentPage', function() {

        var start = ($scope.currentPage -1) * $scope.itemsPerPage + 1;
        dataFactory.get_scopelines_by_envname($scope.selected_env_name, start, $scope.itemsPerPage).success(function(data) {

            $scope.scopeline_list = data.Scopelines;
            $scope.firstItem = {};
            $scope.firstItemView = false;
            console.log($scope.scopeline_list);

        });
    });
}

function CreateScopeController($scope, $http, $uibModal, $log, alertService) {

    $scope.create_scopeline_info = true;
//    $scope.scopeline_create_alert = false;

    $scope.animationEnabled = true;
    $scope.items = [];
    $scope.selected_user_list_str = "";
    $scope.selected_user_list = [];
    $scope.selected_env_name = "";
    $scope.selected_components = [];
    $scope.selected_components_str = "";

    var query_envs = function() {

        $scope.start_point = 1;
        $scope.page_size = 1000;

        var api = "/v1/environments?start=" + $scope.start_point + "&page_size=" + $scope.page_size;
        var url = protocol+host+":"+port+api;

        $http.get(url).success( function(response) {

            $scope.envs = response.envs;

//            var count = response.count;

//            page_num = Math.ceil(count/$scope.page_size);
//
//            for (i=1, $scope.pages = []; i<= page_num; i++) {
//                $scope.pages.push(i);
//            }
        });
    }

    query_envs();

    $scope.show_members = function (size) {
        // request members
        var modalInstance = $uibModal.open({
            animation: $scope.animationEnabled,
            templateUrl : 'selectMemberModal.html',
            size: size,
            controller: "UserModalInstanceCtrl",
            resolve: {
                items : function() {
                    return $scope.items;
                }
            }
        });

        modalInstance.result.then(function(selectedItems) {

            for (var i=0 ; i < selectedItems.length; i++ ) {
                var item = selectedItems[i];

                if (item != null) {
                    $scope.selected_user_list.push(item);
                    $scope.selected_user_list_str +=  item.UserName + ';';
                }
            }
            console.log($scope.selected_user_list);

        }, function() {
            $log.info('Modal dimissed at: ' + new Date());
        });
    }


    $scope.show_components = function(size) {

        var modalInstance = $uibModal.open({
            animation: $scope.animationEnabled,
            templateUrl : 'selectComponentsModal.html',
            size: size,
            controller: "ComsModalInstanceCtrl",
            resolve: {
                items : function() {
                    return $scope.items;
                }
            }
        });

        modalInstance.result.then(function(selectedItems) {

            for (var i=0 ; i < selectedItems.length; i++ ) {
                var item = selectedItems[i];

                if (item != null) {
                    $scope.selected_components.push(item);
                    $scope.selected_components_str += item.Name + '-' + item.Version + '.' + item.ReleaseNo + '.' + item.BuildNo +'-'+ item.Branch + '.zip;';
                }
            }
            console.log($scope.selected_components);

        }, function() {
            $log.info('Modal dimissed at: ' + new Date());
        });
    }
//    var get_dbmigration_id_by_commituid = function(uid) {
//
//    }

    $scope.create_env = function() {

        // api : /v1/scopeline/create
        // json request body :
        // {
        //  'Name' : 'ttt1',
        //  'Version': '1.0',
        //  'CIMaturity' : 'draft',
        //  'Description' : 'xxxxx0000ooooo',
        //  'UrtrackerIssueIDs' : '4567,3345',
        //  'MeetMemberIDs' : '1,36',
        //  'RiskAssessmentOfMeeting' : '风险接受',
        //  'EnvironmentID' : 4,
        //  'DBMigrationID' : 1
        // }

        var meet_member_ids = '';

        var environment_id = 0;
//        $scope.dbmigration_commitUID;
        var dbmigration_id = 0;

        for(var i=0; i< $scope.selected_user_list.length; i++ ) {
            var userid = $scope.selected_user_list[i].UserID
            meet_member_ids += userid + ',';
        }

        meet_member_ids = meet_member_ids.substring(0, meet_member_ids.length-1);

        for (var i=0; i < $scope.envs.length ; i++ ) {
            console.log($scope.envs[i].Name);
            if ($scope.envs[i].Name == $scope.selected_env_name) {
                environment_id = $scope.envs[i].EnvironmentID;
                console.log(environment_id);
            }
        }

        var api = "/v1/scopeline/create";
        var url = protocol + host + ":" + port + api;
        var header = {'Content-Type': 'application/x-www-form-urlencoded'};

        var post_data = {
            'Name' : $scope.scopeline_name,
            'Version' : $scope.scopeline_version,
            'CIMaturity' : $scope.selected_ci,
            'Description' :  $scope.scopeline_desc,
            'UrtrackerIssueIDs' : $scope.urtracker_issue,
            'MeetMemberIDs' : meet_member_ids,
            'RiskAssessmentOfMeeting' : $scope.selected_riskofassesment,
            'EnvironmentID' : environment_id,
            'DBMigrationID' : dbmigration_id
        }

        console.log(post_data);

//        if (post_data.Name == '' ) {
//            alert('表单没有填写完整!');
//            return
//        }

        $http({
                method: 'post',
                url : url,
                data : post_data,
                headers: header

        }).success(function(response){
             if (response.ChangeState=='success') {
                $scope.create_scopeline_info = false;

                 alertService.add('success', '<h4 class="text-center alert alert-success">创建scopeline成功~等待3秒钟!</h4>')
                setTimeout(function() {
                    alertService.closeAlert(0);
                    location.href='#/scopeline_insts'
                }, 3000);
             } else {
                 alertService.add('warning', '出错了~');
             }
        }).error(function(response) {
                alertService.add('error', '创建失败! 错误消息为: ' + response);
        }
        );
    }
}

index_app.controller('UserModalInstanceCtrl', function($scope, $http, $uibModalInstance, items) {

    $scope.user_list = items;

    var page_num = 0;

    $scope.start_point = 1;
    $scope.page_size = 10;
    $scope.current_page = 1;
    $scope.selected = [];

    var send_request = function() {

        $scope.start_point = ($scope.current_page -1) * $scope.page_size + 1 ;

        var api = "/v1/users?start=" + $scope.start_point + "&page_size=" + $scope.page_size;

        var url = protocol+host+":"+port+api;

        $http.get(url).success( function(response) {

            $scope.user_list = response.Users;
            console.log($scope.user_list);

            var count = response.Count;

            page_num = Math.ceil(count/$scope.page_size);

            for (i=1, $scope.pages = []; i<= page_num; i++) {
                $scope.pages.push(i);
            }
        });
    }

    send_request();

    $scope.set_page = function () {
        $scope.current_page = this.page_no;
        //alert("current_page : "  + $scope.current_page);
        send_request();
    }

    $scope.prev_page = function() {

        if ($scope.current_page == 1) {
            return
        }
        $scope.current_page -= 1;
        send_request()
    }

    $scope.next_page = function () {
        if ($scope.current_page == page_num) {
            return
        }
        $scope.current_page += 1;
        send_request()
    }

    $scope.ok = function () {
        $uibModalInstance.close($scope.selected);
    }

    $scope.cancel = function () {
        $uibModalInstance.close();
    }

    $scope.reset = function() {
        $scope.selected = []
    }

    $scope.selected_and_push = function(user, chk_status) {

        if (chk_status == true && user != null) {

            user_tmp = {
                'UserID' : user.UserID,
                'UserName' : user.LastName + user.FirstName
            }

            for (var i = 0; i < $scope.selected.length; i++ ) {
                if ($scope.selected[i].UserID == user_tmp.UserID) {
                    return
                }
            }
            $scope.selected.push(user_tmp);
        }
    }
});


index_app.controller('ComsModalInstanceCtrl', function($scope, $http, $uibModalInstance, items) {
    $scope.coms = items;
    $scope.title = "组建列表";

    var page_num = 0;

    $scope.start_point = 1;
    $scope.page_size = 10;
    $scope.current_page = 1;
    $scope.selected = [];

    var send_request = function() {

        $scope.start_point = ($scope.current_page -1) * $scope.page_size + 1 ;

        var api = "/v1/components?start=" + $scope.start_point + "&page_size=" + $scope.page_size;

        var url = protocol+host+":"+port+api;

        $http.get(url).success( function(response) {

            $scope.com_list = response.Components;

//            console.log($scope.com_list);

            var count = response.Count;

            page_num = Math.ceil(count/$scope.page_size);

            for (i=1, $scope.pages = []; i<= page_num; i++) {
                $scope.pages.push(i);
            }
        });
    }

    send_request();

    $scope.set_page = function () {
        $scope.current_page = this.page_no;
        //alert("current_page : "  + $scope.current_page);
        send_request();
    }

    $scope.prev_page = function() {

        if ($scope.current_page == 1) {
            return
        }
        $scope.current_page -= 1;
        send_request()
    }

    $scope.next_page = function () {
        if ($scope.current_page == page_num) {
            return
        }
        $scope.current_page += 1;
        send_request()
    }

    $scope.ok = function () {
        $uibModalInstance.close($scope.selected);
    }

    $scope.cancel = function () {
        $uibModalInstance.close();
    }

    $scope.reset = function() {
        $scope.selected = []
    }

    $scope.selected_and_push = function(com, chk_status) {

        if (chk_status == true && com != null) {

//            com = {
//                'ComponentID' : com.ComponentID,
//                'Name' : user.LastName + user.FirstName
//            }

            for (var i = 0; i < $scope.selected.length; i++ ) {
                if ($scope.selected[i].ComponentID == com.ComponentID) {
                    return
                }
            }
            $scope.selected.push(com);
        }
    }
});

function CompareScopelineController($scope) {

    $scope.compare_scopeline_display = true;

}


function ScopelineInitializeController($scope, dataFactory) {

    $scope.selected_env_name = 'TestEnv';

    var start = 1;
    var size = 1000;
    dataFactory.get_envs(start, size).success(function(data) {
        $scope.envs = data.envs;
    });

    $scope.initialize = function() {
        dataFactory.get_env_by_id($scope.$scope.env_id).success(function(data) {
            
        });
        dataFactory.init_scopeline_by_env_id()
    }
}


